Skip to content

chore: serve the profiling endpoints on their own listener - #358

Open
mennatnaga wants to merge 3 commits into
mainfrom
menna/debug-listener
Open

chore: serve the profiling endpoints on their own listener#358
mennatnaga wants to merge 3 commits into
mainfrom
menna/debug-listener

Conversation

@mennatnaga

@mennatnaga mennatnaga commented Aug 11, 2026

Copy link
Copy Markdown
Member

Stacked on #357.

The profiling endpoints are moved onto their own mux and listener. Giving a listener a nil handler makes net/http fall back to http.DefaultServeMux, which carries whatever any linked package registered on it, so the profiling port would answer unrelated endpoints.

The listener only starts when PPROF_ADDR is set, so leaving it unset turns profiling off without a rebuild.

It binds wherever PPROF_ADDR points, with no loopback default. In the fleet that protection comes from the docker port mapping rather than from here.

@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@tzdybal tzdybal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefix env variables with SHINZO_ for consistency with other flags and to distinguish from standard Go stdlib flags.

Splitting listen and serve can improve error handling very much - we can stop immediately if listen address is invalid, instead of running with single line of error (not even in log).

Comment thread cmd/main.go Outdated
Comment on lines +38 to +42
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.HandleFunc("/GET debug/pprof/", pprof.Index)
mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile)
mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)

Comment thread cmd/main.go
// serveDebug starts the debug listener on addr. The listener is what makes the endpoints
// reachable, so leaving the address unset turns them off without a rebuild.
func serveDebug(addr string) {
if os.Getenv("PPROF_BLOCK_MUTEX") != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if os.Getenv("PPROF_BLOCK_MUTEX") != "" {
if os.Getenv("SHINZO_PPROF_BLOCK_MUTEX") != "" {

Comment thread cmd/main.go Outdated
}

go func() {
fmt.Fprintf(os.Stderr, "debug endpoints listening on %s\n", addr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should use logger here.

Comment thread cmd/main.go
}

func main() {
if addr := os.Getenv("PPROF_ADDR"); addr != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if addr := os.Getenv("PPROF_ADDR"); addr != "" {
if addr := os.Getenv("SHINZO_PPROF_ADDR"); addr != "" {

Comment thread cmd/main.go
runtime.SetMutexProfileFraction(1)
}

srv := &http.Server{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we split listen and serve, we can synchronously notify about invalid pprof server address

Base automatically changed from menna/signal-shutdown to main September 1, 2026 11:11
@mennatnaga
mennatnaga marked this pull request as ready for review September 2, 2026 10:50
@mennatnaga
mennatnaga requested a review from dvncan as a code owner September 2, 2026 10:50
@mennatnaga mennatnaga self-assigned this Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants